package com.gtt.lazy.controller;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

/**
 * Action基类
 * 
 * @author 高甜甜
 * @date 2009-7-9
 */
@SuppressWarnings("unchecked")
public class BaseAction extends ActionSupport implements SessionAware,
		ServletRequestAware, RequestAware, ServletResponseAware, Preparable {

	private static final long serialVersionUID = 1L;

	private HttpServletRequest httpServletRequest;

	private HttpServletResponse httpServletResponse;

	private Map request;

	private Map session;

	protected int page;

	protected int pageCount;

	public BaseAction() {
	}

	public void setPagination(int count, int size, int page) {
		setPage(page < 1 ? 1 : page);
		int p = count / size;
		if (count % size > 0)
			p = p + 1;
		setPageCount(p);
		if (pageCount == 0) {
			setPage(0);
			setPageCount(0);
		}
	}

	/**
	 * 获取默认错误图片路径
	 * 
	 * @return
	 */
	String getImageErrorPath() {
		return ServletActionContext.getServletContext().getRealPath(
				File.separator + "images" + File.separator + "error");
	}

	/**
	 * 获取图片类型
	 * 
	 * @param fileName
	 * @return
	 */
	String getMimeType(String fileName) {
		return "image/" + ImageUtil.getSuffix(fileName);
	}

	public HttpServletRequest getHttpServletRequest() {
		return httpServletRequest;
	}

	public HttpServletResponse getHttpServletResponse() {
		return httpServletResponse;
	}

	public Map getRequest() {
		return request;
	}

	public HttpServletRequest getServletRequest() {
		return httpServletRequest;
	}

	public HttpServletResponse getServletResponse() {
		return httpServletResponse;
	}

	public Map getSession() {
		return session;
	}

	public void prepare() throws Exception {
	}

	public void setRequest(Map request) {
		this.request = request;
	}

	public void setServletRequest(HttpServletRequest request) {
		httpServletRequest = request;
	}

	public void setServletResponse(HttpServletResponse servletResponse) {
		httpServletResponse = servletResponse;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

}